<template>
  <div class="user" style="padding: 0px;">
    <el-card>
      <el-button size="small" icon="Plus" @click="addClick" v-auth="'/auth/role/add'" plain>添加角色</el-button>
      <el-table size="small" :data="tableData" border style="width: 100%;margin-top:5px;" v-loading="loading" stripe>
        <el-table-column prop="roleId" label="角色表示" align="center"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template #default="scope">
            <el-switch v-auth="'/auth/role/status'" v-model="scope.row.active" size="small" active-text="启用" inactive-text="禁用" @change="onChange(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center"></el-table-column>
        <el-table-column prop="createTime" label="添加时间" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template #default="scope">
            <el-button @click="editClick(scope.row)" type="text" size="small" v-auth="'/auth/role/edit'" icon="Edit">编辑</el-button>
            <el-button @click="deleteClick(scope.row)" type="text" size="small" v-auth="'/auth/role/delete'" icon="Delete" style="color:red;">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import AuthApi from "@/api/auth.js";
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*获取列表*/
    getTableList() {
      let self = this;
      self.loading = true;
      AuthApi.roleList({}, true)
        .then((res) => {
          self.loading = false;
          self.tableData = res.data;
          self.tableData.forEach(item=>{
            item.active = item.status === 0
          })
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    onChange(row) {
       const postBody = {
        roleId: row.roleId,
        status:row.active ? 0 : 1,
       }
       const $this = this 
       AuthApi.changeRoleStatus(postBody)
       .then(result=>{
         const {code,msg} = result
         if (code ===1) {
          $this.getTableList()
          ElMessage.success(msg)
         } else {
          ElMessage.error(msg)
         }
       })
       console.table(postBody)
    },

    /*打开添加*/
    addClick() {
      this.$router.push("/auth/role/add");
    },

    /*打开编辑*/
    editClick(row) {
      let self = this;
      this.$router.push({
        path: "/auth/role/edit",
        query: {
          roleId: row.roleId,
        },
      });
    },

    /*刷新心也*/
    refresh() {
      this.reload();
      // 直接使用reload方法
    },

    /*删除*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          self.loading = true;
          AuthApi.roleDelete(
            {
              roleId: row.roleId,
            },
            true
          )
            .then((data) => {
              self.loading = false;
              if (data.code == 1) {
                ElMessage({
                  message: "恭喜你，该角色删除成功",
                  type: "success",
                });
                self.getTableList();
              } else {
                self.loading = false;
              }
            })
            .catch((error) => {
              self.loading = false;
            });
        })
        .catch(() => {});
    },
  },
};
</script>

<style></style>
